<template>
  <div class="channel-edit">
    <van-cell :border="false">
      <div slot="title" class="title-text">我的频道</div>
      <van-button
        class="edit-btn"
        size="mini"
        round
        type="danger"
        plain
        @click="isEdit = !isEdit"
        >{{ isEdit ? '完成' : '编辑' }}</van-button
      >
    </van-cell>
    <van-grid class="my-grid" :gutter="10">
      <van-grid-item
        class="grid-item"
        v-for="(item, index) in channel"
        :key="index"
        @click="onChannelClick(channel, index)"
      >
        <van-icon
          slot="icon"
          name="clear"
          v-show="isEdit && !fiexdChannels.includes(item.id)"
        ></van-icon>
        <span
          :class="{ active: activeIndex === index }"
          slot="text"
          class="text"
          >{{ item.name }}</span
        >
      </van-grid-item>
    </van-grid>
    <van-cell :border="false">
      <div slot="title" class="title-text">频道推荐</div>
    </van-cell>
    <van-grid class="recomment-grid" :gutter="10">
      <van-grid-item
        icon="plus"
        class="grid-item"
        v-for="(item, index) in recommendChannele"
        :key="index"
      >
        <span class="text" slot="text" @click="onAddChannel(item)">{{
          item.name
        }}</span>
      </van-grid-item>
    </van-grid>
  </div>
</template>

<script>
import {
  getAllChannels,
  getUserPartChannels,
  deleteUserChannel
} from '@/api/user.js'
import { mapState } from 'vuex'
import { setItem } from '@/utils/storage.js'
export default {
  name: 'ChannelEdit',
  props: {
    channel: {
      type: Array,
      required: true
    },
    activeIndex: {
      type: Number,
      required: true
    }
  },
  data () {
    return {
      allChannels: [], // 所有频道
      isEdit: false, // 编辑按钮
      fiexdChannels: [0] // 设置不可操作的标签
    }
  },
  computed: {
    ...mapState(['user']),
    recommendChannele () {
      return this.allChannels.filter((channel) => {
        return !this.channel.find((Mychannel) => Mychannel.id === channel.id)
      })
    }
  },
  created () {
    this.loadAllChannels()
  },
  methods: {
    async loadAllChannels () {
      try {
        const { data } = await getAllChannels()
        this.allChannels = data.data.channels
      } catch (error) {
        this.$toast('获取所有频道失效')
      }
    },
    async onAddChannel (item) {
      // eslint-disable-next-line vue/no-mutating-props
      this.channel.push(item)
      // 数据持久化
      if (this.user) {
        try {
          await getUserPartChannels({
            id: item.id,
            seq: this.channel.length
          })
        } catch (err) {
          this.$toast('保存失败，请稍后再试')
        }
      } else {
        setItem('TOUTIAO_CHANNELS', this.channel)
      }
    },
    onChannelClick (channel, index) {
      // 判断是编辑状态还是完成状态
      if (this.isEdit) {
        // 如果是固定频道，则不删除
        if (this.fiexdChannels.includes(channel.id)) {
          return false
        }
        // 删除频道项
        // eslint-disable-next-line vue/no-mutating-props
        this.channel.splice(index, 1)
        // this.$emit('updata-active', index)
        // 如果要删除的激活频道之前的频道，则更新激活的频道项
        if (index <= this.activeIndex) {
          this.$emit('updata-active', this.activeIndex - 1)
        }
        // 参数1：要删除的元素的开始索引
        // 参数2：要删除的个数，如果不指定，则从参数 1 开始一直删除
        // 让激活频道索引 - 1
      } else {
        console.log(111)
        this.$emit('updata-active', index, false)
      }
      this.deleteChannel(channel)
    },
    async deleteChannel (channel) {
      try {
        if (this.user) {
          await deleteUserChannel(channel.id)
        } else {
          setItem('TOUTIAO_CHANNELS', this.channel)
        }
      } catch (err) {
        this.$toast('操作失败，稍后重试')
      }
    }
  }
}
</script>

<style scoped lang="less">
.channel-edit {
  padding: 85px 0;

  .title-text {
    font-size: 32px;
    color: #333;
  }

  .edit-btn {
    width: 104px;
    height: 48px;
    font-size: 26px;
    color: #f85959;
    border: 1px solid #f85959;
  }

  /deep/ .grid-item {
    width: 160px;
    height: 86px;
    .van-grid-item__content {
      white-space: nowrap;
      background-color: #f4f5f6;
      .van-grid-item__text,
      .text {
        font-size: 22px;
        color: #222;
        margin-top: 4px !important;
      }
      .active {
        color: red;
      }
    }
  }

  /deep/ .my-grid {
    .grid-item {
      .van-icon-clear {
        position: absolute;
        right: -10px;
        top: -10px;
        font-size: 30px;
        color: #cacaca;
        z-index: 2;
      }
    }
  }

  /deep/ .recomment-grid {
    .grid-item {
      width: 160px;
      height: 86px;
      .van-grid-item__text,
      .text {
        font-size: 22px;
        color: #222;
        margin-top: 4px !important;
      }
      .van-grid-item__content {
        flex-direction: row;
        .van-icon-plus {
          font-size: 22px;
          line-height: 22px;
          margin-right: 6px;
        }
      }
    }
  }
}
/deep/.van-grid-item__icon-wrapper {
  position: unset !important;
  .active {
    font-size: 18px;
    color: #b3b5ce !important;
  }
}
// .van-grid-item__icon-wrapper {
//           /deep/
//         }
</style>
